<?php defined('SYSPATH') or die('No direct script access.');

class Controller_Pc_Api_Standard_Visa extends Controller_Pc_Api_Base
{

    public function before()
    {
        parent::before();
    }

    /*
     * 获取轮播图广告
     */
    public function action_get_rolling_ad()
    {
        $result = DB::select()->from('xcx_ads')->where('prefix', '=', 's_visa_index')
            ->order_by('displayorder', 'asc')->execute()->as_array();
        foreach ($result as &$v) {
            //判断产品类型
            if ($v['typeid'] == 8) {
                $v['url'] = '/stpage/visa/pages/visaDetail/visaDetail';
                $mod = DB::select('litpic')->from('visa');
            }
            if (!empty($mod)) {
                $row = $mod->where('id', '=', $v['proid'])->execute()->current();
                //增加图片绝对访问地址
                $v['litpic'] = Model_Api_Standard_System::reorganized_resource_url($row['litpic']);
            }
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    // 签证热门国家
    public function action_hot_visa_country()
    {
        $result = Taglib_Visa::area(array('row' => 9,'flag'=>'hot','minprice'=>'true'));
        foreach ($result as &$item)
        {
            $item['countrypic'] = Common::img($item['countrypic']);
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    public function action_visa_list()
    {
        $result = Taglib_Visa::query(array('row' => 10,'flag'=>'order'));

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    //查询条件
    public function action_query_condition()
    {
        $result = array(
            'sort' => array(
                array('id' => 0, 'name' => '综合排序'),
                array('id' => 1, 'name' => '价格从低到高'),
                array('id' => 2, 'name' => '价格从高到低'),
                array('id' => 3, 'name' => '销量最高'),
                array('id' => 4, 'name' => '产品推荐')
            ),
        );
        //属性
        $all = array(array('id'=> '0','kindname'=>'全部'));
        $filterAttr= DB::select('id', 'kindname')->from('visa_kind')->where('isopen', '=', 1)->order_by(DB::expr('ifnull(displayorder,9999)'), 'asc')->execute()->as_array();
        $result['filterAttr'] = array_merge ($all,$filterAttr);
        // 签证区域
        $area = Model_Api_Standard_Visa::get_area_query(0);
        foreach($area as &$v){
            $v['child'] = Model_Api_Standard_Visa::get_area_query($v['id']);
        }
        $result['area'] = $area ;

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    public function action_general_query()
    {
        $path = $this->request_body->content->path;
        list($data['area'], $data['sort_type'], $data['visatype'], $data['page']) = explode('-', $path);
        $data['keyword'] = $this->request_body->content->keyword;
        $result = Model_Api_Standard_Visa::search_result($data);

        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }


    public  function action_get_service_show()
    {
        $id = intval($this->request_body->content->id);
        $productid = intval($this->request_body->content->productid);
        $userid = intval($this->request_body->content->userid);
        $id = empty($id) ? $productid : $id;
        $service = Model_Api_Standard_Visa::get_info($id,$userid);
        //获得积分策略
        $service['jifentprice_info'] = Model_Jifen_Price::get_used_jifentprice($service['jifentprice_id'], 8);
        $service['jifenbook_info'] = Model_Jifen::get_used_jifenbook($service['jifenbook_id'], 8);
        $service['jifencomment_info'] = Model_Jifen::get_used_jifencomment(8);
        //去掉过期优惠券
        if(!empty($service['coupon_list']))
        {
            foreach ($service['coupon_list'] as $key=>$vel)
            {
                if($vel['endtime']!=0 && $vel['endtime']<=time())
                {
                    unset($service['coupon_list'][$key]);
                }
            }
            $service['coupon_list']=array_values($service['coupon_list']);
        }
        $this->send_datagrams($this->client_info['id'], $service, $this->client_info['secret_key']);
    }

    public function action_create_order()
    {
        //联系人
        $linkman = Common::remove_xss($this->request_body->content->linkman);
        //预订时间
        $usedate = Common::remove_xss($this->request_body->content->usedate);
        //手机号
        $linktel = Common::remove_xss($this->request_body->content->linktel);
        //电子邮箱
        $linkemail = Common::remove_xss($this->request_body->content->linkemail);
        //备注信息
        $remark = Common::remove_xss($this->request_body->content->remark);
        //产品id
        $id = Common::remove_xss($this->request_body->content->id);
        //预订数量
        $dingnum = Common::remove_xss($this->request_body->content->dingnum);
        //会员id
        $memberId = $this->request_body->content->memberid;
        Common::session('member', array('mid' => $memberId));
        $address = Common::remove_xss($this->request_body->content->address);

        //二次验证
        $info = Model_Visa::visa_detail_id(intval($id));
        if ($info['paytype'] == '3')//这里补充一个当为二次确认时,修改订单为未处理状态.
        {
            $info['need_confirm'] = 1;
            $info['status'] = 0;
        }
        else
        {
            $info['need_confirm'] = 0;
            $info['status'] = 1;
        }
        //积分抵现.
        $jifentprice = 0;
        $useJifen = 0;
        //积分评论
        $jifencomment_info = Model_Jifen::get_used_jifencomment(8);
        $jifencomment = empty($jifencomment_info) ? 0 : $jifencomment_info['value'];
        //合并生成订单
        $ordersn = Product::get_ordersn('08');
        $arr = array(
            'ordersn' => $ordersn,
            'webid' => 0,
            'typeid' => 8,
            'productautoid' => $id,
            'productaid' => $info['aid'],
            'productname' => $info['title'],
            'litpic' => $info['litpic'],
            'price' => $info['price'],
            'childprice' => $info['childprice'],
            'jifentprice' => $jifentprice,
            'jifenbook' => $info['jifenbook_id'],
            'jifencomment' => $jifencomment,
            'paytype' => $info['paytype'],
            'dingjin' => $info['dingjin'],
            'usedate' => $usedate,
            'departdate' => $info['departdate'],
            'addtime' => time(),
            'memberid' => $memberId,
            'dingnum' => $dingnum,
            'childnum' => 0,
            'oldprice' => 0,
            'oldnum' => 0,
            'linkman' => $linkman,
            'linktel' => $linktel,
            'linkemail' => $linkemail,
            'linkidcard' => '',
            'suitid' => 0,
            'remark' => $remark,
            'status' => $info['status'] ? $info['status'] : 0,
            'usejifen' => $useJifen,
            'needjifen' => 0,
            'refund_restriction'=>$info['refund_restriction'],//退改条件
            'receiver_address_id' => $address ? (int)$address : 0,
            'need_confirm' => $info['need_confirm']
        );

        /*--------------------------------------------------------------优惠券信息------------------------------------------------------------*/
        //优惠券判断
        $croleid = $this->request_body->content->couponid;;
        if ($croleid) {
            $cid = DB::select('cid')->from('member_coupon')->where("id=$croleid")->execute()->current();
            $totalprice = Model_Coupon::get_order_totalprice($arr);
            $ischeck = Model_Coupon::check_samount($croleid, $totalprice, 8, $info['id'], $usedate);
            if ($ischeck['status'] == 1) {
                Model_Coupon::add_coupon_order($cid, $ordersn, $totalprice, $ischeck, $croleid); //添加订单优惠券信息
            }
            else {
                $this->send_datagrams($this->client_info['id'], array('status' => false, 'msg' => 'coupon  verification failed!'), $this->client_info['secret_key']);
                exit;
            }
        }
        /*-----------------------------------------------------------------优惠券信息--------------------------------------*/


        //添加订单,跳转支付
        $out = St_Product::add_order($arr, 'Model_Visa');
        if ($out) {
            $orderInfo = Model_Member_Order::order_info($ordersn);
            $result = array(
                'status' => true,
                'orderinfo' => $orderInfo
            );
            St_Product::event($ordersn);
            $this->send_datagrams($this->client_info['id'],$result,$this->client_info['secret_key']);
        }
        else
        {
            $result = array(
                'status' => false,
                'msg' => $out['msg']
            );
            $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);

        }
    }

    //获得地址信息
    public function action_getaddress()
    {
        $addressid=Common::remove_xss($this->request_body->content->addressid);
        $receiver_address = DB::select()->from('member_address')->where('id','=',$addressid)->execute()->current();
        $this->send_datagrams($this->client_info['id'], $receiver_address, $this->client_info['secret_key']);
    }
}